package com.youmingxing.network;

import android.content.Context;
import android.util.Log;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.tencent.upload.Const;
import com.tencent.upload.UploadManager;
import com.tencent.upload.task.IUploadTaskListener;
import com.tencent.upload.task.impl.PhotoUploadTask;
import com.tencent.upload.task.impl.VideoUploadTask;
import com.youmingxing.data.GlobalDataManager;

import org.apache.http.Header;
import org.json.JSONObject;

/**
 * @author evilyin(ChenZhixi)
 * @since 15/11/7
 */
public class TXYUploadManager {
    static UploadManager uploadPhotoManager;
    static UploadManager uploadVideoManager;

    public static void init(Context context) {
        uploadVideoManager = new UploadManager(context, com.youmingxing.utils.Const.APP_ID, Const.FileType.Video, null);
        uploadPhotoManager = new UploadManager(context, com.youmingxing.utils.Const.APP_ID, Const.FileType.Photo, null);
    }

    public static void uploadPhoto(String path, IUploadTaskListener context) {
        final PhotoUploadTask photoUploadTask = new PhotoUploadTask(path, context);
        photoUploadTask.setBucket(com.youmingxing.utils.Const.bucketImage);
        AccountApi.getPhotoSign(new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                if (response.optInt("status") == 200) {
                    photoUploadTask.setAuth(response.optJSONObject("data").optString("sign"));
                }
                uploadPhotoManager.upload(photoUploadTask);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                uploadPhotoManager.upload(photoUploadTask);
            }
        });

    }

    public static void uploadPhoto(byte[] data, IUploadTaskListener context) {
        final PhotoUploadTask photoUploadTask = new PhotoUploadTask(data, context);
        photoUploadTask.setBucket(com.youmingxing.utils.Const.bucketImage);
        AccountApi.getPhotoSign(new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                if (response.optInt("status") == 200) {
                    photoUploadTask.setAuth(response.optJSONObject("data").optString("sign"));
                }
                uploadPhotoManager.upload(photoUploadTask);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                uploadPhotoManager.upload(photoUploadTask);
            }
        });
    }

    public static void uploadVideo(String path, IUploadTaskListener listener) {
        final VideoUploadTask videoUploadTask = new VideoUploadTask(com.youmingxing.utils.Const
                .bucketVideo, path, "/" + GlobalDataManager.userId + System
                .currentTimeMillis
                        () + ".mp4", null, null, listener);
        AccountApi.getVedioSign(new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                if (response.optInt("status") == 200) {
                    videoUploadTask.setAuth(response.optJSONObject("data").optString("sign"));
                }
                uploadVideoManager.upload(videoUploadTask);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                uploadVideoManager.upload(videoUploadTask);
            }
        });
    }

}
